package us.icitap.commandcenter.ui.dispatcher;

import us.icitap.commandcenter.CommandCenterUI;
import us.icitap.commandcenter.business.Result;
import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.business.ejb.persistence.CallStatusEnum;
import us.icitap.commandcenter.listeners.ManageCallListener;
import us.icitap.commandcenter.ui.custom.ButtonsLayout;
import us.icitap.commandcenter.ui.custom.CustSaveButton;
import us.icitap.commandcenter.ui.custom.CustTextArea;
import us.icitap.commandcenter.ui.custom.CustTextField;
import us.icitap.commandcenter.utils.HandleNotification;

import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.server.ThemeResource;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Window.CloseEvent;
import com.vaadin.ui.Window.CloseListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class UpdateVerificationWindow extends Window implements ClickListener{

	
	private final CustTextField patrolsTextField = new CustTextField("Patrulla qe caktohet per verifikim");
	private final CustTextField givenOrdersTextField = new CustTextField("Urdherat qe jepen");
	private final CustTextArea verificationNotesTextArea = new CustTextArea("Shenime mbi verifikimin e telefonates");
	private final CustSaveButton saveButton = new CustSaveButton("RUAJ");
	
	BeanFieldGroup<Call> binder = new BeanFieldGroup<Call>(Call.class);
	private Call call;
	
	private ManageCallListener manageCallListener;
	
	String previousVerificationNotes;
	String previousPatrols;
	String previousGivenOrders;
	
	ThemeResource infoImgResource = new ThemeResource("img/icons/blue/Info.png");
	
	final Embedded embPreviousPatrols = new Embedded(null,infoImgResource);
	final Embedded embPreviousVerNotes = new Embedded(null, infoImgResource);
	final Embedded embPrevoiusGivenOrders = new Embedded(null,infoImgResource);
	
	private boolean updateSucceed = false;
	
	public UpdateVerificationWindow(ManageCallListener manageCallListener,Call call) {
		
		this.manageCallListener = manageCallListener;
	
		this.setCall(call);
		
		this.setCaption("Te dhena shtese per telefonaten");
		this.setModal(true);
		this.center();
		this.setResizable(false);
		this.binder.setItemDataSource(call);
		this.binder.bind(this.patrolsTextField, "patrols");
		this.binder.bind(this.givenOrdersTextField, "givenOrders");
		this.binder.bind(this.verificationNotesTextArea, "verificationNotes");
		
		VerticalLayout vl = new VerticalLayout();
		vl.setMargin(true);
		
		this.patrolsTextField.setDescription("Shtoni patrullat qe dergohen");
		this.givenOrdersTextField.setDescription("Shtoni urdherat qe jepen");
		this.verificationNotesTextArea.setDescription("Shtoni shenimet shtese ne lidhje me telefonaten dhe verifikimin");
		
		HorizontalLayout hl1 = new HorizontalLayout();
		hl1.addComponent(patrolsTextField);
		hl1.addComponent(embPreviousPatrols);
		
		
		
		HorizontalLayout hl2 = new HorizontalLayout();
		hl2.addComponent(verificationNotesTextArea);
		hl2.addComponent(embPreviousVerNotes);
		this.verificationNotesTextArea.setWidth("300px");
		
		HorizontalLayout hl3 = new HorizontalLayout();
		hl3.addComponent(givenOrdersTextField);
		hl3.addComponent(embPrevoiusGivenOrders);
		this.givenOrdersTextField.setWidth("300");
		
		vl.addComponent(hl1);
		vl.addComponent(hl3);
		vl.addComponent(hl2);
		
		
		final ButtonsLayout buttonsLayout = new ButtonsLayout("500px");
		buttonsLayout.addComponent(saveButton);
		buttonsLayout.setComponentAlignment(saveButton, Alignment.MIDDLE_RIGHT);
		vl.addComponent(buttonsLayout);
		
		
		this.setContent(vl);
		saveButton.setImmediate(true);
		saveButton.setDescription("Ne momentin qe do shtypni butonin te dhenat do ruhen ne DB");
		this.saveButton.addClickListener(this);
	
		this.addCloseListener(new CloseListener() {
			
			@Override
			public void windowClose(CloseEvent e) {
				if(updateSucceed) return;
				UpdateVerificationWindow.this.call.setPatrols(previousPatrols);
				UpdateVerificationWindow.this.call.setGivenOrders(previousGivenOrders);
				UpdateVerificationWindow.this.call.setVerificationNotes(previousVerificationNotes);
			}
		});

	}
	
	
	public void setCall(Call call){
		this.call = call;
		this.updateSucceed = false;
		if(call.getPatrols() != null ){
			this.previousPatrols = new String(call.getPatrols());
			call.setPatrols(null);
			this.embPreviousPatrols.setDescription("<h3>Patrullat e derguara</h3>" + this.previousPatrols);
		}
		else{
			this.previousPatrols = "";
			this.embPreviousPatrols.setDescription("<h3>Akoma nuk eshte derguar asnje patrulle</h3>");
		}
			
		
		if(call.getGivenOrders() != null ){
			this.previousGivenOrders = new String(call.getGivenOrders());
			call.setGivenOrders(null);
			this.embPrevoiusGivenOrders.setDescription("<h3>Urdherat e dhene</h3>" + this.previousGivenOrders);
		}
		else{
			this.previousGivenOrders = "";
			this.embPrevoiusGivenOrders.setDescription("<h3>Akoma nuk eshte dhene asnje urdher</h3>");
		}
			
		
		if(call.getVerificationNotes() != null ){
			this.previousVerificationNotes = new String(call.getVerificationNotes());
			call.setVerificationNotes(null);
			this.embPreviousVerNotes.setDescription("<h3>Shenimet e meparshme</h3>" + this.previousVerificationNotes);
		}
		else{
			this.previousVerificationNotes = "";
			this.embPreviousVerNotes.setDescription("<h3>Akoma nuk eshte rregjistruar asnje shenim</h3>");
		}
			
			
		
		this.binder.setItemDataSource(call);
	}
	@Override
	public void buttonClick(ClickEvent event) {
		try {
			binder.commit();
			
			if(call.getPatrols() != null ){
				if(!this.previousPatrols.equals(""))
					call.setPatrols(this.previousPatrols + ", " + call.getPatrols());
			}
			else{
				call.setPatrols(this.previousPatrols);
			}
			
			
			
			if(call.getGivenOrders() != null){
				if(!this.previousGivenOrders.equals(""))
					call.setGivenOrders(this.previousGivenOrders + ", " + call.getGivenOrders());
			}
				
			else{
				call.setGivenOrders(this.previousGivenOrders);
			}
			
		
			
			if(call.getVerificationNotes() != null){
				if(!this.previousVerificationNotes.equals(""))
					call.setVerificationNotes(this.previousVerificationNotes + "<br/>" + call.getVerificationNotes());
			}
				
			else{
				call.setVerificationNotes(this.previousVerificationNotes);
			}
			
			
			this.manageCallListener.updateCallInformation(call);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
